//
//  JCNSButtonView.swift
//  JCTest3
//
//  Created by Chen on 10/10/2022.
//

import Cocoa

class JCNSButtonView: JCBaseBottomView {
    
    private lazy var pushOnPushOffButton2: NSButton = NSButton()

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

//        self.setupViewBackgroundColor(color: NSColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0))
        // Drawing code here.
        self.setButtonsWithBezelStyle()
        self.setButtonsWithButtonType()
        self.setButtonWithImage()
    }
    
    func setButtonsWithBezelStyle() {
        
        // 按钮按不同边框
//        case rounded = 1
//
//        case regularSquare = 2
//
//        case disclosure = 5
//
//        case shadowlessSquare = 6
//
//        case circular = 7
//
//        case texturedSquare = 8
//
//        case helpButton = 9
//
//        case smallSquare = 10
//
//        case texturedRounded = 11
//
//        case roundRect = 12
//
//        case recessed = 13
//
//        case roundedDisclosure = 14
//
//        @available(macOS 10.7, *)
//        case inline = 15
        let button1 = NSButton(frame: NSRect(x: 10, y: 10, width: 60, height: 40))
        self.addSubview(button1)
        // 设置一个背景颜色便于对比
        button1.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button1.title = "rounded" // 文本一般用这个
        button1.bezelStyle = .rounded
        
        let button2 = NSButton(frame: NSRect(x: 80, y: 10, width: 60, height: 40))
        self.addSubview(button2)
        button2.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button2.title = "regularSquare" // 图标一般用这个
        button2.bezelStyle = .regularSquare
        
        let button3 = NSButton(frame: NSRect(x: 150, y: 10, width: 60, height: 40))
        self.addSubview(button3)
//        button3.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
//        button3.title = "disclosure"
        button3.title = ""
        button3.bezelStyle = .disclosure
        
        let button4 = NSButton(frame: NSRect(x: 220, y: 10, width: 60, height: 40))
        self.addSubview(button4)
        button4.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button4.title = "shadowlessSquare"
        button4.bezelStyle = .shadowlessSquare
        
        let button5 = NSButton(frame: NSRect(x: 290, y: 10, width: 60, height: 40))
        self.addSubview(button5)
//        button5.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
//        button5.title = "circular"
        button5.title = ""
        button5.bezelStyle = .circular
        
        let button6 = NSButton(frame: NSRect(x: 360, y: 10, width: 60, height: 40))
        self.addSubview(button6)
        button6.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button6.title = "texturedSquare"
        button6.bezelStyle = .texturedSquare
        
        let button7 = NSButton(frame: NSRect(x: 430, y: 10, width: 60, height: 40))
        self.addSubview(button7)
        button7.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
//        button7.title = "helpButton"
        button7.title = ""
        button7.bezelStyle = .helpButton
        
        let button8 = NSButton(frame: NSRect(x: 500, y: 10, width: 60, height: 40))
        self.addSubview(button8)
        button8.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button8.title = "smallSquare"
        button8.bezelStyle = .smallSquare
        
        let button9 = NSButton(frame: NSRect(x: 570, y: 10, width: 60, height: 40))
        self.addSubview(button9)
        button9.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button9.title = "texturedRounded"
        button9.bezelStyle = .texturedRounded
        
        let button10 = NSButton(frame: NSRect(x: 640, y: 10, width: 60, height: 40))
        self.addSubview(button10)
        button10.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button10.title = "roundRect"
        button10.bezelStyle = .roundRect
        
        let button11 = NSButton(frame: NSRect(x: 710, y: 10, width: 60, height: 40))
        self.addSubview(button11)
        button11.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button11.title = "recessed"
        button11.bezelStyle = .recessed
        
        let button12 = NSButton(frame: NSRect(x: 780, y: 10, width: 60, height: 40))
        self.addSubview(button12)
        button12.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
//        button12.title = "roundedDisclosure"
        button12.title = ""
        button12.bezelStyle = .roundedDisclosure
        
        let button13 = NSButton(frame: NSRect(x: 850, y: 10, width: 60, height: 40))
        self.addSubview(button13)
        button13.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button13.title = "inline"
//        button13.contentTintColor = NSColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
//        button13.isBordered = false // 设置为false时没有
        button13.layer?.cornerRadius = 4.0
        button13.layer?.borderWidth = 1.0
        button13.layer?.borderColor = NSColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0).cgColor
        button13.bezelStyle = .inline
        button13.showsBorderOnlyWhileMouseInside = true // 鼠标放上去时会显示内部边框
        
        
    }
    
    func setButtonsWithButtonType() {
        
//        case momentaryLight = 0
//
//        case pushOnPushOff = 1
//
//        case toggle = 2
//
//        case `switch` = 3
//
//        case radio = 4
//
//        case momentaryChange = 5
//
//        case onOff = 6
//
//        case momentaryPushIn = 7
//
//        @available(macOS 10.10.3, *)
//        case accelerator = 8
//
//        @available(macOS 10.10.3, *)
//        case multiLevelAccelerator = 9
        
        let button1 = NSButton(frame: NSRect(x: 10, y: 80, width: 80, height: 40))
        self.addSubview(button1)
        button1.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button1.title = "momentaryLight"
        button1.setButtonType(.momentaryLight)
        
        let button2 = NSButton(frame: NSRect(x: 100, y: 80, width: 80, height: 40))
        self.addSubview(button2)
        button2.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button2.setButtonType(.pushOnPushOff)
        button2.title = "on"
        button2.alternateTitle = "off"
        button2.image = NSImage(named: "start")
        button2.imagePosition = .imageRight
        self.pushOnPushOffButton2 = button2
        
        let button3 = NSButton(frame: NSRect(x: 190, y: 80, width: 80, height: 40))
        self.addSubview(button3)
        button3.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button3.setButtonType(.toggle)
        // 用一个toggle类型的按钮来控制button2的两种状态
        // 点击在两种状体下切换
        button3.title = "start"
        button3.alternateTitle = "stop"
        
        let button4 = NSButton(frame: NSRect(x: 280, y: 80, width: 80, height: 40))
        self.addSubview(button4)
        button4.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button4.title = "switch"
        button4.setButtonType(.switch)
        
        let button5 = NSButton(frame: NSRect(x: 370, y: 80, width: 80, height: 40))
        self.addSubview(button5)
        button5.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button5.title = "radio"
        button5.setButtonType(.radio)
        
        let button6 = NSButton(frame: NSRect(x: 460, y: 80, width: 80, height: 40))
        self.addSubview(button6)
        button6.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button6.setButtonType(.momentaryChange) // 这种类型的按钮，在按着不动时有第二种状态
        button6.title = "off" // 松开鼠标时显示的标题
        button6.alternateTitle = "on" // 按着的时候显示的标题
        
        let button7 = NSButton(frame: NSRect(x: 550, y: 80, width: 80, height: 40))
        self.addSubview(button7)
        button7.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button7.title = "onOff"
        button7.setButtonType(.onOff)
        
        let button8 = NSButton(frame: NSRect(x: 640, y: 80, width: 80, height: 40))
        self.addSubview(button8)
        button8.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0))
        button8.title = "momentaryPushIn"
        // 设置按钮标题颜色
        button8.setTitleColorAndFont(title: "momentaryPushIn", color: .blue, fontNum: 18, isBold: true)
        button8.setButtonType(.momentaryPushIn)
        
        // os版本需要大于10.10.3
//        let button9 = NSButton(frame: NSRect(x: 570, y: 80, width: 60, height: 40))
//        self.addSubview(button9)
//        button9.setButtonType(.accelerator)
//
//        let button10 = NSButton(frame: NSRect(x: 640, y: 80, width: 60, height: 40))
//        self.addSubview(button10)
//        button10.setButtonType(.multiLevelAccelerator)
        
    }
    
    // 设置button的图片
    func setButtonWithImage() {
        
        let button1 = NSButton(frame: NSRect(x: 10, y: 130, width: 120, height: 40))
//        button1.image = NSImage(named: "dish_008")
//        button1.imagePosition = .imageBelow
        button1.title = "title"
        button1.setButtonType(.momentaryPushIn)
        button1.bezelStyle = .inline
        button1.setTitleColorAndFont(title: "title", color: .blue, fontNum: 18, isBold: true)
        // 鼠标悬停时显示的文字
        button1.toolTip = "mouse in button"
        button1.layer?.cornerRadius = 8.0
        button1.layer?.borderWidth = 2.0
        button1.layer?.borderColor = NSColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0).cgColor
        button1.alignment = .center
//        button1.setupViewBackgroundColor(color: .yellow)
        
//        button1.imageScaling = .scaleAxesIndependently
//        button1.isBordered = false
        
        self.addSubview(button1)
        
        /*
        case noImage = 0

        case imageOnly = 1

        case imageLeft = 2

        case imageRight = 3

        case imageBelow = 4

        case imageAbove = 5

        case imageOverlaps = 6

        @available(macOS 10.12, *)
        case imageLeading = 7

        @available(macOS 10.12, *)
        case imageTrailing = 8
        */
        let bW = 100.0
        let bH = 100.0
        let xOffset = 10.0
        for i in 0..<7 {
            
            let btn = NSButton(frame: NSRect(x: xOffset * Double((i + 1)) + bW * Double(i), y: 180, width: bW, height: bH))
            btn.image = NSImage(named: "dish_001")
            self.addSubview(btn)
            switch i {
            case 0:
                btn.title = "noImage"
                btn.imagePosition = .noImage
            case 1:
                btn.title = "imageOnly"
                btn.imagePosition = .imageOnly
                btn.setButtonType(.toggle) // 点一下on, 再点一下off
                btn.alternateImage = NSImage(named: "dish_002")
                btn.toolTip = "鼠标按下会切换图片"
            case 2:
                btn.title = "imageLeft"
                btn.imagePosition = .imageLeft
                btn.setButtonType(.momentaryChange) // 鼠标按下on，鼠标松开off
                btn.alternateImage = NSImage(named: "dish_003")
                btn.toolTip = "鼠标按下会改变图片"
            case 3:
                btn.title = "imageRight"
                btn.imagePosition = .imageRight
            case 4:
                btn.title = "imageBelow"
                btn.imagePosition = .imageBelow
            case 5:
                btn.title = "imageAbove"
                btn.imagePosition = .imageAbove
            case 6:
                btn.title = "imageOverlaps"
                btn.imagePosition = .imageOverlaps
            default:
                print("")
            }
            
        }
    }
    
}

// MARK: - 事件
extension JCNSButtonView {
    
    @objc func toggleButton3Clicked(btn: NSButton) {
        
    }
    
    @objc func imageButtonClicked(btn: NSButton) {
        print("=========imageButtonClicked===========")
    }
    
}


